Convert DataTable to Dictionary in C#

132

Convert DataTable to Dictionary in C# -

private List<Dictionary<string, object>> DataTableToDictionary(DataTable dt)
{
    var dictionaries = new List<Dictionary<string, object>>();
    foreach (DataRow row in dt.Rows)
    {
        Dictionary<string, object> dictionary = Enumerable.Range(0, dt.Columns.Count).ToDictionary(i => dt.Columns[i].ColumnName, i => row.ItemArray[i]);
        dictionaries.Add(dictionary);
    }

    return dictionaries;
}

Comments

Submit
0 Comments